package cn.edu.tju.elm.repository;

import cn.edu.tju.elm.model.Order;
import cn.edu.tju.elm.model.Business;
import cn.edu.tju.core.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o WHERE o.customer = :customer AND o.deleted = false")
    List<Order> findByCustomer(User customer);
    
    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o WHERE o.customer.id = :customerId AND o.deleted = false")
    List<Order> findByCustomerId(Long customerId);
    
    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o WHERE o.id = :id AND o.customer = :customer AND o.deleted = false")
    Optional<Order> findByIdAndCustomer(Long id, User customer);
    
    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o WHERE o.business.id = :businessId AND o.deleted = false")
    List<Order> findByBusinessId(Long businessId);
    
    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o WHERE o.business = :business AND o.deleted = false")
    List<Order> findByBusiness(Business business);
    
    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o WHERE o.customer = :customer AND o.orderState = :orderState AND o.deleted = false")
    List<Order> findByCustomerAndOrderState(@Param("customer") User customer, @Param("orderState") Integer orderState);
    
    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o WHERE o.orderState = :orderState AND o.deleted = false")
    List<Order> findByOrderState(Integer orderState);

    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o " +
           "LEFT JOIN FETCH o.customer " +
           "LEFT JOIN FETCH o.business " +
           "LEFT JOIN FETCH o.deliveryAddress " +
           "LEFT JOIN FETCH o.orderDetailets od " +
           "LEFT JOIN FETCH od.food " +
           "WHERE o.id = :id AND o.customer = :customer AND o.deleted = false")
    Optional<Order> findByIdAndCustomerWithDetails(@Param("id") Long id, @Param("customer") User customer);

    // 修改：添加 deleted = false 条件
    @Query("SELECT DISTINCT o FROM Order o LEFT JOIN FETCH o.orderDetailets od LEFT JOIN FETCH od.food WHERE o.customer = :customer AND o.deleted = false")
    List<Order> findByCustomerWithDetails(@Param("customer") User customer);
    
    // 修改：添加 deleted = false 条件
    @Query("SELECT o FROM Order o LEFT JOIN FETCH o.orderDetailets od LEFT JOIN FETCH od.food WHERE o.id = :id AND o.deleted = false")
    Optional<Order> findByIdWithDetails(@Param("id") Long id);
    
    // 新增：查找未删除的订单
    @Query("SELECT o FROM Order o WHERE o.id = :id AND o.deleted = false")
    Optional<Order> findByIdAndDeletedIsFalse(@Param("id") Long id);
}